package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import entity.PageResult;
import entity.QueryPageBean;
import entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

;import java.util.List;

/**
 * @Description:
 * @Author: yp
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

  @Reference
    private PermissionService permissionService;

  //角色需要的权限菜单:
  @RequestMapping("/findAll")
  public Result findAll(){
      try {
          List<Permission> list =  permissionService.findAll();
          return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
      } catch (Exception e) {
          e.printStackTrace();
          return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
      }
  }



    //权限管理分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult =  permissionService.findPage(queryPageBean);
        System.out.println("返回的分页结果=="+pageResult);
        return  pageResult;
    }

    //菜单管理新增项:
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    //权限项编辑,表单回显
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Permission permission =  permissionService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //编辑权限项.
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try {
            permissionService.edit(permission);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }


    //删除权限项:
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            permissionService.delete(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }
}
